package com.awanp3.nimbus.base.service.impl;

import com.awanp3.nimbus.common.core.utils.MapstructUtils;
import com.awanp3.nimbus.common.core.utils.StringUtils;
import com.awanp3.nimbus.common.mybatis.core.page.TableDataInfo;
import com.awanp3.nimbus.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.awanp3.nimbus.base.domain.bo.BaseCompanyBo;
import com.awanp3.nimbus.base.domain.vo.BaseCompanyVo;
import com.awanp3.nimbus.base.domain.BaseCompany;
import com.awanp3.nimbus.base.mapper.BaseCompanyMapper;
import com.awanp3.nimbus.base.service.IBaseCompanyService;

import java.util.List;
import java.util.Collection;

/**
 * 公司Service业务层处理
 *
 * @author 20014170
 * @date 2025-09-05
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class BaseCompanyServiceImpl implements IBaseCompanyService {

    private final BaseCompanyMapper baseMapper;

    /**
     * 查询公司
     *
     * @param companyId 主键
     * @return 公司
     */
    @Override
    public BaseCompanyVo queryById(Long companyId){
        return baseMapper.selectVoById(companyId);
    }

    /**
     * 分页查询公司列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 公司分页列表
     */
    @Override
    public TableDataInfo<BaseCompanyVo> queryPageList(BaseCompanyBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BaseCompany> lqw = buildQueryWrapper(bo);
        Page<BaseCompanyVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的公司列表
     *
     * @param bo 查询条件
     * @return 公司列表
     */
    @Override
    public List<BaseCompanyVo> queryList(BaseCompanyBo bo) {
        LambdaQueryWrapper<BaseCompany> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BaseCompany> buildQueryWrapper(BaseCompanyBo bo) {
        LambdaQueryWrapper<BaseCompany> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(BaseCompany::getCompanyId);
        lqw.eq(StringUtils.isNotBlank(bo.getCompanyCode()), BaseCompany::getCompanyCode, bo.getCompanyCode());
        lqw.like(StringUtils.isNotBlank(bo.getCompanyName()), BaseCompany::getCompanyName, bo.getCompanyName());
        lqw.eq(StringUtils.isNotBlank(bo.getCountryCode()), BaseCompany::getCountryCode, bo.getCountryCode());
        lqw.like(StringUtils.isNotBlank(bo.getCountryName()), BaseCompany::getCountryName, bo.getCountryName());
        lqw.eq(StringUtils.isNotBlank(bo.getTimeZoneId()), BaseCompany::getTimeZoneId, bo.getTimeZoneId());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), BaseCompany::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增公司
     *
     * @param bo 公司
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(BaseCompanyBo bo) {
        BaseCompany add = MapstructUtils.convert(bo, BaseCompany.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setCompanyId(add.getCompanyId());
        }
        return flag;
    }

    /**
     * 修改公司
     *
     * @param bo 公司
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(BaseCompanyBo bo) {
        BaseCompany update = MapstructUtils.convert(bo, BaseCompany.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BaseCompany entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除公司信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
